//
//  XZPaymentDetailVC.m
//  多条目
//
//  Created by Henry on 2018/1/10.
//  Copyright © 2018年 kmwl. All rights reserved.
//

#import "XZPaymentDetailVC.h"
#import "ZYDatePickerView.h"
#import "XZStudentPayVC.h"
#import "HMRenewdetailsModel.h"
#import "CustomDayDatePicker.h"


// View 圆角和加边框
#define kViewBorderRadius(View, Radius, Width, Color)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES];\
[View.layer setBorderWidth:(Width)];\
[View.layer setBorderColor:[Color CGColor]]

// View 圆角
#define ViewRadius(View, Radius)\
\
[View.layer setCornerRadius:(Radius)];\
[View.layer setMasksToBounds:YES]

@interface XZPaymentDetailVC ()<CustomAlertDelegete>
@property (weak, nonatomic) IBOutlet UILabel *startDate;
@property (weak, nonatomic) IBOutlet UILabel *endDate;
@property (weak, nonatomic) IBOutlet UILabel *totalAmount;
@property (weak, nonatomic) IBOutlet UIButton *payNum;
@property (weak, nonatomic) IBOutlet UIButton *noPayNum;
@property (strong, nonatomic) CustomDayDatePicker *datePicker;

@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *moreButton;
@end

@implementation XZPaymentDetailVC {
    UILabel *_label;
    NSInteger _mark;//标记类型
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self setNetWork];
}

- (void)setupUI {
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(popPickDate:)];
    _startDate.userInteractionEnabled = YES;
    [_startDate addGestureRecognizer:tap1];
    
     UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(popPickDate:)];
    _endDate.userInteractionEnabled = YES;
    [_endDate addGestureRecognizer:tap2];

    _startDate.layer.cornerRadius = 5;
    _startDate.clipsToBounds = YES;
    
    _endDate.layer.cornerRadius = 5;
    _endDate.clipsToBounds = YES;
    
    [_moreButton enumerateObjectsUsingBlock:^(UIButton *obj, NSUInteger idx, BOOL * _Nonnull stop) {
       [obj SG_imagePositionStyle:(SGImagePositionStyleRight) spacing:10];
    }];
    
    self.startDate.text = [NSString getCurrentDateYearMD];
    self.endDate.text = [NSString yearMonthDay];
    [self.startDate mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.offset(85*KScale);
    }];
}



#pragma mark - 点击弹日期选择器
- (void)popPickDate:(UITapGestureRecognizer *)tap {
    
    if (tap.self.view == _startDate) {
        NSLog(@"开始");
        [self ClickTimeOfMark:1 Title:@"开始时间"];
    } else {
        NSLog(@"结束");
        [self ClickTimeOfMark:2 Title:@"结束时间"];
    }
}

#pragma mark - 查询日期
- (IBAction)queryDate:(id)sender {
    
    [self setNetWork];
}

#pragma mark - 已经支付的
- (IBAction)alreadyPay:(id)sender {
    XZStudentPayVC *payVC = [[XZStudentPayVC alloc] init];
    payVC.isFlang = @"YES";
    payVC.dateArr = @[_startDate.text, _endDate.text];
    [self.navigationController pushViewController:payVC animated:YES];
}

#pragma mark - 没有支付的列表
- (IBAction)noPay:(id)sender {
    XZStudentPayVC *payVC = [[XZStudentPayVC alloc] init];
    payVC.dateArr = @[_startDate.text, _endDate.text];
    [self.navigationController pushViewController:payVC animated:YES];
}

#pragma mark - 网络请求
- (void)setNetWork{
    
    NSString *url = [NSString stringWithFormat:@"yxhUser.do?action=getStuPayment&startTime=%@&endTime=%@",self.startDate.text,self.endDate.text];
    NSLog(@"%@ %@",LLBaseURL(url),[AccountTool shareAccountTool].currentAccount.authCode);
    [SVProgressHUD setDefaultStyle:(SVProgressHUDStyleCustom)];
    [SVProgressHUD  setBackgroundColor:ColorRGB(4, 4, 4)];
    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
    [SVProgressHUD showWithStatus:LLLoading];
    
    [NetManager Get:LLBaseURL(url) Auth:YES Params:nil Success:^(id Obj) {
        [SVProgressHUD dismiss];
        if ([[Obj valueForKey:@"code"]integerValue] == 1000) {
            
            NSDictionary *dict = [Obj valueForKey:@"t"];
          HMRenewdetailsModel *model = [[HMRenewdetailsModel alloc] initWithDict:dict];
            self.totalAmount.text = [NSString stringWithFormat:@" ¥ %@",model.money];
            [self.payNum setTitle:[NSString stringWithFormat:@"   缴费人数: %@人",model.ispay] forState:(UIControlStateNormal)];
            [self.noPayNum setTitle:[NSString stringWithFormat:@"   未缴费人数: %@人",model.notpay] forState:(UIControlStateNormal)];
        }
        
    } Fail:^(NSError *Error) {
        
        [SVProgressHUD dismiss];
    }];
    
}

#pragma mark - Func
- (void)ClickTimeOfMark:(NSInteger)Mark Title:(NSString *)Title {
    JXAlertview *alert = [[JXAlertview alloc]initWithFrame:CGRectMake(30, kScreenHeight/2-100, kScreenWidth - 60, 200)];
    alert.delegate = self;
    [alert initwithtitle:Title andcommitbtn:@"确定" andStr:@"0"];
    [alert addSubview:self.datePicker];
    [alert show];
    _mark = Mark;
}

- (void)btnindex:(int)index :(int)tag {
    
    if (index == 2) {
        if (_mark == 1) {
            _startDate.text = [NSString stringWithFormat:@"%d-%02d-%02d",_datePicker.year,_datePicker.month,_datePicker.day];
        }else if (_mark == 2){
            
            _endDate.text = [NSString stringWithFormat:@"%d-%02d-%02d",_datePicker.year,_datePicker.month,_datePicker.day];
        }
    }
}

-(NSDate *)getPriousorLaterDateFromDate:(NSDate *)date withMonth:(int)month {
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    
    [comps setMonth:month];
    
    NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSDate *mDate = [calender dateByAddingComponents:comps toDate:date options:0];
    
    return mDate;
    
}


#pragma mark - Get
- (CustomDayDatePicker *)datePicker {
    
    if (_datePicker == nil) {
        _datePicker = [[CustomDayDatePicker alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth - 60, 140)];
    }
    return _datePicker;
}

@end
